package com.wgts.wgts.mvp.view.activity.message;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.netmodle.resp.MessageListEntity;
import com.wgts.wgts.tools.CustomImageView;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/10/2217:25
 *
 */public class MessageDetailsAdapter extends BaseLoadMoreAdapter<MessageDetailsAdapter.ViewHold> {
    private Context context;
    private List<MessageListEntity> listEntities;

    public MessageDetailsAdapter(Context context,List<MessageListEntity> listEntities) {
        super(context);
        this.context = context;
        this.listEntities = listEntities;
    }

    @Override
    public void onBindBodyViewHolder(@NonNull ViewHold viewHolder, final int i) {
        CustomImageView.getInstance().customView(context, viewHolder.informImage, 9/(double)16);
        if (listEntities.get(i).coverPlan != null && !listEntities.get(i).coverPlan.equals("")){
            viewHolder.informImage.setVisibility(View.VISIBLE);
            MoHuGlide.getInstance().glideImg(context, listEntities.get(i).coverPlan, viewHolder.informImage);
        } else {
            viewHolder.informImage.setVisibility(View.GONE);
        }
        if (listEntities.get(i).content != null && !listEntities.get(i).content.equals("")){
            viewHolder.watchDetailsLy.setVisibility(View.VISIBLE);
        }else {
            viewHolder.watchDetailsLy.setVisibility(View.GONE);
        }
       viewHolder.messageType.setText(listEntities.get(i).messageType);
        viewHolder.messageContent.setText(listEntities.get(i).title);
        viewHolder.messageTime.setText(listEntities.get(i).messageTime);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listEntities.get(i).content != null && !listEntities.get(i).content.equals("")){
                    Intent intent = new Intent(context, InformActivity.class);
                    intent.putExtra(InformActivity.CONTENT, listEntities.get(i).content);
                    if (!(context instanceof Activity)) {
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    }
                    context.startActivity(intent);
                }
            }
        });
    }

    @NonNull
    @Override
    public ViewHold onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.message_details_item, viewGroup, false));
    }

    @Override
    public int getBodyItemCount() {
        return listEntities.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        return 0;
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.message_time)
        TextView messageTime;
        @BindView(R.id.message_type)
        TextView messageType;
        @BindView(R.id.inform_image)
        ImageView informImage;
        @BindView(R.id.message_content)
        TextView messageContent;
        @BindView(R.id.watch_details_ly)
        RelativeLayout watchDetailsLy;

        private View itemView;
        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }
}
